﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using LogisticsBLL;
using System.Data;

namespace WebLosgic
{
    public partial class AddUser : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {

                if (Session["names"] != null)
                {
                    string ming = Session["names"].ToString();
                    Names.Text = ming;
                    DdlBind();
                    
                    

                }
                else
                {
                    Page.ClientScript.RegisterStartupScript(GetType(), "", "alert('请先去登录');location.href='login.aspx'", true);
                }
            remove();
           }
            
        }


        public void DdlBind()
        {
            SqlDataReader read = UserBLL.droplie();
            DropRole.DataSource = read;
            DropRole.DataTextField = "RoleName";
            DropRole.DataValueField = "RoleID";
            DropRole.DataBind();
            ListItem item = new ListItem();
            item.Text = "-请选择-";
            item.Value = "0";
            DropRole.Items.Add(item);
            DropRole.SelectedValue = "0";
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            
            string name = username.Value;
            string sex = RadioSex.SelectedValue;
            string Email = useremail.Value;
            string phone = userphone.Value;
            string acount = userQ.Value;
            string pwd = TexPwder1.Text;
            if (name == "")
            {
                Response.Write("<script>alert('请输入用户名')</script>");
                return;
            }
            if (Email == "") { Response.Write("<script>alert('请输入Email')</script>");
                return;
            }
            if (phone == "") { Response.Write("<script>alert('请电话号码')</script>");
                return;
            }
            if (acount == "") { Response.Write("<script>alert('请输入账号')</script>");
                return;
            }
            if (pwd == "") { Response.Write("<script>alert('请输入密码')</script>");
                return;
            }
            UserBLL ubl = new UserBLL();
                int am = ubl.PDcunzaName(name);
                if (am>0)
                {
                    //Response.Write("<script>alert('"+str+"')</script>");
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('用户名已存在')", true);
                }
            int ad = ubl.PDcunzaAccount(acount);
            if (ad > 0)
            {
                //Response.Write("<script>alert('"+str+"')</script>");
                Page.ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('账号已存在')", true);
            }
            if (TexPwdor.Text != TexPwder1.Text)
                    {
                        Response.Write("<script>alert('两次密码不同')</script>");
                        //Page.ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('两次密码不同，请重新输入')", true);
                    }
                    else
                    {
                        int roleid = Convert.ToInt32(DropRole.SelectedValue);
                        string Isdel = RadioIsdel.SelectedValue;
                        int result = UserBLL.AddUse(name, sex, acount, pwd, phone, Email, roleid, Isdel);
                        string Names = Session["names"].ToString();
                        string Behavor = "" + Names + ": 添加用户";
                        int TypeId = 2;
                        int UserID = Convert.ToInt32(Session["UserIDs"]);
                        string Parameters = @"" + username.Value + "," + userphone.Value + "," + RadioSex.SelectedValue;
                        string ProcName = "p_AddUser";
                        HttpRequest request = HttpContext.Current.Request;
                        string IP = "192.168.0.1";
                        int IsExcePion = 0;
                        if (result > 0)
                        {
                            IsExcePion = 0;
                            int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                            if (a > 0)
                            {
                                ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('添加成功');location.href='Users.aspx';", true);
                            }

                        }
                        else
                        {
                            IsExcePion = 1;
                            int a = ProcBLL.InserSys(Behavor, TypeId, UserID, Parameters, ProcName, IP, IsExcePion);
                            if (a > 0)
                            {
                                ClientScript.RegisterStartupScript(this.GetType(), "error", "alert('添加失败')", true);
                            }
                        }
                    
                

            }
      }


        public void remove()
        {
            username.Value="";
            RadioSex.SelectedValue="0";
            useremail.Value="";
            userphone.Value="";
            userQ.Value=" ";
            TexPwder1.Text="";
            DropRole.SelectedValue="0";
            RadioIsdel.SelectedValue="1";
        }


      


        protected void BtnReturn_Click(object sender, EventArgs e)
        {
            remove();
        }
    }
}